﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using AFS.VisitationRequestTool.Addin.Common;
using Microsoft.Office.Interop.Outlook;
using AFS.VisitationRequestTool.Addin.Entities;
using AFS.VisitationRequestTool.Addin.Properties;
using AFS.VisitationRequestTool.Addin.Components;
using AFS.VisitationRequestTool.Addin.Variables;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
//  {
//      return new RibbonSubmitVR();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace AFS.VisitationRequestTool.Addin
{
    [ComVisible(true)]
    public class RibbonSubmitVRCustom : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        AppointmentItem appointmentItem;
        VisitationRequest visitationRequest;
        ProgressForm currentProgressForm;

        Settings settings = new Settings();
        RequestDetailsFormRegion requestDetailsFormRegion;

        public RibbonSubmitVRCustom()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("AFS.VisitationRequestTool.Addin.RibbonSubmitVR.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        private void InitializeRibbon()
        {
            //Logger.Verbose("Entered");
            //Inspector inspector = this.Context as Inspector;
            //// requestDetailsRegion = Globals.FormRegions[inspector].AFSRequestDetailsForm;

            //// Load handler hides Custom Ribbon if not meeting instance
            //if (inspector.CurrentItem is AppointmentItem &&
            //    ((AppointmentItem)inspector.CurrentItem).MeetingStatus != OlMeetingStatus.olNonMeeting) //Check to make sure we're dealing with a meeting item
            //{
            //    appointmentItem = (AppointmentItem)inspector.CurrentItem;

            //    if ((appointmentItem.ItemProperties[Property.RequestSubmitted] != null && (bool)appointmentItem.ItemProperties[Property.RequestSubmitted].Value) ||
            //        (appointmentItem.ItemProperties[Property.Filename] != null && appointmentItem.ItemProperties[Property.Filename].Value.ToString().Length > 0))
            //    {
            //        Logger.Verbose(String.Format("Request already submitted."));

            //        btnUploadToSharepoint.Enabled = false;
            //    }
            //    else
            //    {
            //        btnUploadToSharepoint.Enabled = true;
            //        //Globals.ThisAddIn.Application.ActiveInspector()
            //        requestDetailsFormRegion = Globals.FormRegions[inspector].AFSRequestDetailsForm;
            //    }
            //    //Globals.RibbonLoaded = true;
            //}
            //else // If some kind of item other than appointment type, hide add-in elements as well
            //{
            //    this.tab1.Visible = false;
            //    this.group1.Visible = false;
            //    this.btnUploadToSharepoint.Visible = false;
            //}
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
